package com.lingnan.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.Itinerary;
import com.lingnan.service.ItineraryService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.ItineraryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 梁兴达
 * @Date 2022/3/22 19:49:37
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/itinerary")
@Api("行程表接口")
public class ItineraryController {

    @Autowired
    private ItineraryService itineraryService;



    @PostMapping("/addOrEditItinerary")
    @ApiOperation("增加或者修改行程")
    public Result addOrEditItinerary(@ApiParam(value = "传行程对象，如果是修改将上一个行程对象的id也传回来") @RequestBody Itinerary itinerary){
        if (!ObjectUtils.isEmpty(itinerary))
        {
            boolean b = itineraryService.saveOrUpdate(itinerary);
            if (b){
                return ResultGenerator.genSuccessResult(200,"增加或修改行程成功");
            }
        }
        return ResultGenerator.genErrorResult(201,"增加或修改行程失败");
    }


    @GetMapping("/itineraryList")
    @ApiOperation("显示行程信息")
    public Result itineraryList(@ApiParam(value = "传用户no",example = "201908764240")@RequestParam String no){
        List<Itinerary> itineraries;
        if (!StrUtil.isEmpty(no)){
            itineraries = itineraryService.selectByNo(no);
            return ResultGenerator.genSuccessResult(200,itineraries,"显示行程成功");
        }
        return ResultGenerator.genErrorResult(201,"显示行程失败");
    }


    @PostMapping("/deleteItinerary")
    @ApiOperation("删除行程")
    public Result deleteItinerary(@ApiParam("传行程id和学号no")@RequestBody ItineraryVo itineraryVo){
        if (!ObjectUtils.isEmpty(itineraryVo)) {
            itineraryService.deleteByNoAndId(itineraryVo.getId(),itineraryVo.getNo());
            return ResultGenerator.genSuccessResult(200,"删除行程成功");
        }
        return ResultGenerator.genErrorResult(201,"删除行程失败");
    }

}
